﻿using System;
using System.Linq;
using System.Reflection;

using Snake.Core;

namespace Snake.Mapping
{
    /// <summary>
    /// 实体Map
    /// </summary>
    public class EntityMap
    {
        public Type Type;//实体类型
        public BracketedName Schema;//架构
        public BracketedName Table;//表
        public PropertyMap Key;//主键
        public PropertyMap[] Properties;//属性
        public ReferenceMap[] References;//内部类

        public Delegate Assemble;//组合委托
        /// <summary>
        /// 查找属性的map
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public PropertyMap FindPropertyMap(PropertyInfo propertyInfo)
        {
            if (Properties != null)
                return Properties.FirstOrDefault(mapping => mapping.Property == propertyInfo);
            return null;
        }

        /// <summary>
        /// 查找属性 的内部类的map
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public ReferenceMap FindReferenceMap(PropertyInfo propertyInfo)
        {
            if (Properties != null)
                return References.FirstOrDefault(mapping => mapping.Property == propertyInfo);
            return null;
        }
    }
}